﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace 剧情设计器2._0
{
    public partial class CharacterEditor : Form
    {
        public Character character;
        public CharacterEditor(Character c)
        {
            character = c;
            InitializeComponent();
        }

        void UpdateDisplay()
        {
            edit_name.Text = character.name;
            listView_settings.BeginUpdate();
            listView_settings.Items.Clear();
            foreach(Setting setting in character.settings)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = setting.name;
                lvi.SubItems.Add(setting.type);
                lvi.SubItems.Add(setting.value);
                listView_settings.Items.Add(lvi);
            }
            listView_settings.EndUpdate();

            comboBox_settingtype.BeginUpdate();
            comboBox_settingtype.Items.Clear();
            foreach (string type in Common.setting_types)
            {
                comboBox_settingtype.Items.Add(type);
            }
            comboBox_settingtype.EndUpdate();
        }

        private void CharacterEditorLoad(object sender, EventArgs e)
        {
            UpdateDisplay();
        }

        private void CreateSetting(object sender, EventArgs e)
        {
            string settingname;
            settingname = Interaction.InputBox("请输入新设定的名称", "创建设定", "");
            foreach(Setting setting in character.settings)
            {
                if(settingname == setting.name)
                {
                    MessageBox.Show(string.Format("设定 {0} 已存在！", settingname), "创建设定", MessageBoxButtons.OK);
                    return;
                }
            }
            character.settings.Add(new Setting() { name = settingname });
            UpdateDisplay();
        }

        private void DeleteSetting(object sender, EventArgs e)
        {
            int idx = listView_settings.SelectedIndices[0];
            if (idx < 0) return;
            if(MessageBox.Show(string.Format("确定要删除设定 {0} 吗？", character.settings[idx].name), "删除设定", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                character.settings.RemoveAt(idx);
                UpdateDisplay();
            }
        }

        void updateEditorState(bool state)
        {
            edit_settingname.Enabled = state;
            edit_value.Enabled = state;
            comboBox_settingtype.Enabled = state;
            btn_ok.Enabled = state;
        }

        private void listView_settings_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView_settings.SelectedIndices.Count < 1)
            {
                updateEditorState(false);
                return; 
            }
            int idx = listView_settings.SelectedIndices[0];
            if (idx < 0)
            {
                updateEditorState(false);
                return;
            }
            updateEditorState(true);
            Setting setting = character.settings[idx];
            edit_settingname.Text = setting.name;
            comboBox_settingtype.Text = setting.type;
            edit_value.Text = setting.value;
            listView_settings.Focus();
            //UpdateDisplay();
        }

        private void edit_name_TextChanged(object sender, EventArgs e)
        {
            character.name = edit_name.Text;
        }

        public void updateSetting()
        {
            if (listView_settings.SelectedIndices.Count < 1) return;
            int idx = listView_settings.SelectedIndices[0];
            if (idx < 0) return;
            Setting setting = character.settings[idx];
            setting.name = edit_settingname.Text;
            setting.type = comboBox_settingtype.Text;
            setting.value = edit_value.Text;

            if (!Common.inList(comboBox_settingtype.Text, Common.setting_types) && comboBox_settingtype.Text != "")
            {
                if (MessageBox.Show(string.Format("发现一个未定义的设定类型：{0}\n是否将其添加到设定类型预设中?", comboBox_settingtype.Text), "发现新设定", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Common.setting_types.Add(comboBox_settingtype.Text);
                }
            }

            UpdateDisplay();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            updateSetting();
        }

        private void CharacterEditor_FormClosed(object sender, FormClosedEventArgs e)
        {
            Program.mainform.UpdateDisplay();
        }

        private void btn_export_Click(object sender, EventArgs e)
        {
            CharacterTemplate ct = new CharacterTemplate();
            foreach(Setting setting in character.settings)
            {
                ct.settings.Add(new SettingTemplate { name = setting.name, type = setting.type });
            }

            string filepath;
            if(saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                filepath = saveFileDialog.FileName;
                FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None);
                XmlSerializer xs = new XmlSerializer(typeof(CharacterTemplate));
                xs.Serialize(fs, ct);
                fs.Close();
            }

            
        }

        private void btn_import_Click(object sender, EventArgs e)
        {
            CharacterTemplate ct = new CharacterTemplate();
            string filepath;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filepath = openFileDialog.FileName;
                FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
                XmlSerializer xs = new XmlSerializer(typeof(CharacterTemplate));
                ct = (CharacterTemplate)xs.Deserialize(fs);
                character.settings.Clear();
                foreach(SettingTemplate st in ct.settings)
                {
                    character.settings.Add(new Setting { name = st.name, type = st.type });
                }
                fs.Close();
                UpdateDisplay();
            }
        }
    }
}
